/**
 * Created by gongxun on 16/11/18.
 */

var KwxHallSettingLayer = KwxBaseLayer.extend({
    Panel_center : null,
    Slider_effect : null,
    Slider_music : null,
    ctor : function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxHallSettingLayer.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");

        //关闭
        mo.seekWidgetByName(this.ccsNode, "Button_close").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                this.closeSetting();
            }
        }, this);

        //关闭
        mo.seekWidgetByName(this.ccsNode, "Panel_touch").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                this.closeSetting();
            }
        }, this);

        //退出
        mo.seekWidgetByName(this.ccsNode, "Button_exitGame").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                this.closeSetting();
                mo.fileHelper.setAccessTokenAndLoginNameTimeOut();
                mo.gameWebsocket.setClose();
                mo.sceneHelper.intoLoginLayer();
            }
        }, this);

        this.Slider_effect = mo.seekWidgetByName(this.ccsNode, "Slider_effect");
        this.Slider_music = mo.seekWidgetByName(this.ccsNode, "Slider_music");
        this.Slider_effect.addEventListener(this.sliderEvent, this);
        this.Slider_music.addEventListener(this.sliderEvent, this);

        //effects
        var Image_effect = mo.seekWidgetByName(this.ccsNode, "Image_effect");
        mo.seekWidgetByName(Image_effect, "Image_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)){
                mo.gameUserInfo.setEffectVoice(0);
            }
        }, this);

        mo.seekWidgetByName(Image_effect, "Image_open").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)){
                mo.gameUserInfo.setEffectVoice(100);
            }
        }, this);

        //music
        var Image_music = mo.seekWidgetByName(this.ccsNode, "Image_music");
        mo.seekWidgetByName(Image_music, "Image_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)){
                mo.gameUserInfo.setMusicVoice(0);
            }
        }, this);

        mo.seekWidgetByName(Image_music, "Image_open").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)){
                mo.gameUserInfo.setMusicVoice(100);
            }
        }, this);


    },

    onEnter : function(){
        this._super();

        this.showAction(this.Panel_center);

        this.updateMusicAndEffets(mo.gameUserInfo.mMusicVolume, mo.gameUserInfo.mEffectsVolume);

        mo.eventHelper.add("updateMusicAndEffects", this, function(){
            this.updateMusicAndEffets(mo.gameUserInfo.mMusicVolume, mo.gameUserInfo.mEffectsVolume);
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);

        this._super();
    },

    updateMusicAndEffets : function(musicVolume, effectsVolume){

        mo.seekWidgetByName(this.ccsNode, "Slider_music").setPercent(musicVolume);

        mo.seekWidgetByName(this.ccsNode, "Slider_effect").setPercent(effectsVolume);
    },

    sliderEvent: function (sender, type) {
        switch (type) {
            case ccui.Slider.EVENT_PERCENT_CHANGED:
                if (this.Slider_effect == sender){
                    var slider = sender;
                    var percent = slider.getPercent();
                    percent = parseInt(percent);
                    mo.gameUserInfo.mEffectsVolume = percent;
                    mo.soundHelper.setEffectsVolume(percent);
                }else{
                    var slider = sender;
                    var percent = slider.getPercent();
                    percent = parseInt(percent);
                    mo.gameUserInfo.mMusicVolume = percent;
                    mo.soundHelper.setMusicVolume(percent);
                }
                break;
            default:
                break;
        }
    },

    closeSetting : function(){
        mo.gameUserInfo.setMusicAndEffects(parseInt(this.Slider_music.getPercent()), parseInt(this.Slider_effect.getPercent()));

        this.closeAction(this.Panel_center);
    }
});